package cn.ffcs.product.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.organ.model.Person;
import cn.ffcs.organ.service.PersonService;
import cn.ffcs.product.model.Product;
import cn.ffcs.product.model.ProductManager;
import cn.ffcs.product.service.ProductService;
import cn.ffcs.security.model.User;
import cn.ffcs.security.service.UserService;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.util.context.ApplicationContextUtil;

public class ProductManagerAction<P extends ProductManager> extends
        BaseAction<P>
{

    /** 业务POJO **/
    private P productManager;
    /** 产品服务 **/
    private ProductService productService;
    /** 产品列表数据 **/
    private List<Product> products;
    /** 产品经理列表数据 **/
    private List<P> productManagers;
    /** 用户服务 **/
    private UserService userService;
    /** 人员服务 **/
    private PersonService personService;
    /** 产品列表数据 **/
    private List<User> users;

    /**
     * 初始化业务数据
     * @return
     */
    @SuppressWarnings("unchecked")
    public String init()
    {
        // Criterion cond = Restrictions.eq("dictType.id", 75);
        //users = userService.findAll();
       // products = productService.findAll();

        return SUCCESS;
    }

    /**
     * 保存操作
     * @return
     */
    public String saveOrUpdate()
    {
        // 保存产品资料数据
        baseService.saveOrUpdate(productManager);

        // 初始化数据
        init();
        return SUCCESS;
    }
    
    /**
     * 
     * 根据关键字获取实体数据
     * @return
     */
    public String findById()
    {
        if (productManager != null && productManager.getId() != 0)
        {
            productManager = (P) baseService.findById(productManager.getId());
        }
        init();
        return SUCCESS;
    }
    
    /**
     * 
     * 删除操作
     * @return
     */
    public String remove()
    {
        baseService.remove(productManager);
        return SUCCESS;
    }
    
    /**
     * 
     * 批量删除操作
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        return SUCCESS;
    }


    /**
     * 统一维护默认显示当前用户对应产品-pany-add-090925
     * @return
     */
    public String viewProduct()
    {
        User currUser = ApplicationContextUtil.getUserContextObject().getCurrUser();
        HashMap<String, Object> conds=new HashMap<String, Object>();
        conds.put("person", currUser.getPerson());
        super.setConds(conds);
        productManagers=this.findBy();
        return SUCCESS;
    }

    /**
     * 产品类型视图-pany-add-090925
     * @return
     */
    public String queryProduct()
    {
        HashMap<String, Object> conds=super.getConds();
        if(conds!=null){
            conds.put("product.name", conds.get("name"));
            conds.remove("name");
            super.setConds(conds);
        }
        return super.findByPage();
    }
    
    
    
    /**
     * 
     * 组合查询条件
     * @return
     */
    public Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if (key.equalsIgnoreCase("title")){
                        temp=Restrictions.like(key, "%"+super.getConds().get(key).toString()+"%");
                    }else if (key.equalsIgnoreCase("person.id") || key.equalsIgnoreCase("product.id")){
                        temp=Restrictions.eq(key, Integer.parseInt(super.getConds().get(key).toString()));
                    }else if (key.equalsIgnoreCase("person.name")){
                        //先查找对应人员id
                        Criterion cPerson = Restrictions.like("name", "%"+super.getConds().get(key).toString()+"%");
                        List<Person> persons=personService.findBy(cPerson);
                        if (persons!=null && persons.size()>0){
                            Integer[] personIDs=new Integer[persons.size()];
                            int j=0;
                            for (Person p:persons){
                                personIDs[j]=p.getId();
                                j++;
                            }
                            temp=Restrictions.in("person.id", personIDs);
                        }
                    }else if ( key.equalsIgnoreCase("product.name")){
                        //先查找对应产品ID
                        Criterion cProduct = Restrictions.like("name", "%"+super.getConds().get(key).toString()+"%");
                        List<Product> cproducts=productService.findBy(cProduct);
                        if (cproducts!=null && cproducts.size()>0){
                            Integer[] productIDs=new Integer[cproducts.size()];
                            int j=0;
                            for (Product p:cproducts){
                                productIDs[j]=p.getId();
                                j++;
                            }
                            temp=Restrictions.in("product.id", productIDs);
                        }
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        return cond;
    }
    
    public P getProductManager()
    {
        return productManager;
    }

    public void setProductManager(P productManager)
    {
        this.productManager = productManager;
    }

    /**
     * @return the productService
     */
    public ProductService getProductService()
    {
        return productService;
    }

    /**
     * @param productService the productService to set
     */
    public void setProductService(ProductService productService)
    {
        this.productService = productService;
    }

    /**
     * @return the products
     */
    public List<Product> getProducts()
    {
        return products;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<Product> products)
    {
        this.products = products;
    }

    /**
     * @return the userService
     */
    public UserService getUserService()
    {
        return userService;
    }

    /**
     * @param userService the userService to set
     */
    public void setUserService(UserService userService)
    {
        this.userService = userService;
    }

    /**
     * @return the users
     */
    public List<User> getUsers()
    {
        return users;
    }

    /**
     * @param users the users to set
     */
    public void setUsers(List<User> users)
    {
        this.users = users;
    }

    /**
     * @return the personService
     */
    public PersonService getPersonService()
    {
        return personService;
    }

    /**
     * @param personService the personService to set
     */
    public void setPersonService(PersonService personService)
    {
        this.personService = personService;
    }

    public List<P> getProductManagers()
    {
        return productManagers;
    }

    public void setProductManagers(List<P> productManagers)
    {
        this.productManagers = productManagers;
    }

}
